<?php
session_start ();

$result = array ();
if (empty ( $_SESSION ['username'] ))
	die ( json_encode ( array (
			'success' => false,
			'errors' => 'Not logged in' 
	) ) );
if (! empty ( $_GET ['auctionID'] )) {

	include_once '../pages/config.php';
	$auctionID = ( int ) $_GET ['auctionID'];
	$userN = $_SESSION ['username'];
	$connection = mysql_connect ( $host	, $username, $password ) or die ( json_encode ( array (
			'success' => false,
			'errors' => "could not connect to MySql" . mysql_error () 
	) ) );
	mysql_select_db ( "mydb", $connection ) or die ( json_encode ( array (
			'success' => false,
			'errors' => "could not connect to MySql" . mysql_error () 
	) ) );
	
	$auction_view = @mysql_query ( "select * from auction_view where auctionID=" . $auctionID, $connection );
	if (! $auction_view)
		die ( json_encode ( array (
				'success' => false,
				'errors' => "Auction view query error" 
		) ) );
	
	$user = @mysql_query ( "select username,balance from User where username='" . $userN . "'", $connection );
	if (! $user)
		die ( json_encode ( array (
				'success' => false,
				'errors' => "User query error" 
		) ) );
		
		// insert into Bid table
	$statement = "insert into Bid(auctionID,username) VALUES(" . $auctionID . ",'" . $userN . "')";
	$success = true;
	// check insert into Bid table successful or not
	if (! ($result = @mysql_query ( $statement, $connection ))) {
		
		die ( json_encode ( array (
				'success' => false,
				'errors' => 'Errors occured ' , 
				'query' => $statement 
		) ) );
	}
	
	mysql_close ( $connection );
}
die ( json_encode ( $result ) );